package cycling.taxi.com.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.bean.PositionInfo;
import cycling.taxi.com.bean.UserVerhicle;
import cycling.taxi.com.parse.ParseTraject;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;
import cycling.taxi.com.zxing.android.CaptureActivity;

/**
 * 绑定设备号
 * Created by Administrator on 2017/5/5.
 */
public class BindCarAty extends Activity implements RequestCallback,View.OnClickListener{
    @InjectView(R.id.bind_car_facility)
    EditText mBindCarFacility;
    @InjectView(R.id.bind_car_bt)
    Button mBindCarBt;
    @InjectView(R.id.forward)
    Button mForward;
    @InjectView(R.id.filter_text)
    TextView mFilterText;
    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.title_text)
    TextView mTitleText;
    @InjectView(R.id.xinghao)
    LinearLayout mxinghao;
    @InjectView(R.id.chexinghao)
    TextView mchexinghao;
    @InjectView(R.id.chejiahao)
    LinearLayout mchejiahao;
    @InjectView(R.id.chejia)
    TextView mchejia;
    @InjectView(R.id.myimei)
    LinearLayout myimei;
    @InjectView(R.id.imei)
    TextView mimei;

    private static final String DECODED_CONTENT_KEY = "codedContent";
    private static final String DECODED_BITMAP_KEY = "codedBitmap";
    private static final int REQUEST_CODE_SCAN = 0x0000;

    private String mFacilityCode;
    private String mPhone;
    private boolean isBind=false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUI();

        initData();
        unBindFacilityCode();
        setListener();
    }

    private void initData() {
        mForward.setVisibility(View.VISIBLE);
        mForward.setBackground(getResources().getDrawable(R.drawable.pic_qrcode));
        mFilterText.setVisibility(View.VISIBLE);
        mFilterText.setText("扫一扫");
        mTitleText.setText("绑定设备");
    }

    private void setListener() {
        back.setOnClickListener(this);
        mForward.setOnClickListener(this);
        mBindCarBt.setOnClickListener(this);
    }

    private void initUI() {
        setContentView(R.layout.aty_bindcar);
        ExitApplication.getInstance().addActivity(this);
        ButterKnife.inject(this);
    }
    private void unBindFacilityCode() {
        RequestParams params = new RequestParams.Builder()
                .putParam("mobile", mPhone)
                .putParam("beanJson",SPUtil.getInstance().getData("beanJson"))
                .build();

        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Urls.getunBindFacility();//回去api路径

        RequestUtil.requestData(requestQueue,
                IConstants.FACILITY,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);
    }
    private void BindFacilityCode() {
        RequestParams params = new RequestParams.Builder()
                .putParam("imei", mFacilityCode)
                .putParam("mobile", mPhone)
                .putParam("beanJson",SPUtil.getInstance().getData("beanJson"))
                .putParam("status",SPUtil.getInstance().getData("status"))
                .build();

        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Urls.getBindFacility();//回去api路径

        RequestUtil.requestData(requestQueue,
                IConstants.FACILITY,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);
    }


    @Override
    public void requestSuccess(RequestBean bean, Config config) {
        LogUtil.e("requestSuccess数据="+bean.getStatusCode()+bean.beanJson);
        try {
            UserVerhicle info= new Gson().fromJson(bean.beanJson,UserVerhicle.class);

        switch (bean.getStatusCode()){
            case 0://绑定状态
                mxinghao.setVisibility(View.VISIBLE);
                mchejiahao.setVisibility(View.VISIBLE);
                myimei.setVisibility(View.VISIBLE);
                mchexinghao.setText(info.getPinpaiName());
                mchejia.setText(info.getChejiaNum());
                mimei.setText(info.getImei());
                mBindCarFacility.setEnabled(false);
                mBindCarFacility.setVisibility(View.GONE);
                mForward.setVisibility(View.GONE);
                mFilterText.setVisibility(View.GONE);
                mBindCarBt.setText("解绑");
                break;
            case 1://未绑定状态
                mxinghao.setVisibility(View.GONE);
                mchejiahao.setVisibility(View.GONE);
                myimei.setVisibility(View.GONE);
                mBindCarFacility.setVisibility(View.VISIBLE);
                mBindCarFacility.setText(bean.beanJson);
                mBindCarFacility.setEnabled(true);

                mBindCarBt.setText("绑定");
                break;
            case 2://解绑动作
                mBindCarFacility.setText(bean.beanJson);
                mBindCarFacility.setEnabled(true);
                mBindCarFacility.setVisibility(View.VISIBLE);
                mxinghao.setVisibility(View.GONE);
                mchejiahao.setVisibility(View.GONE);
                myimei.setVisibility(View.GONE);
                mBindCarBt.setText("绑定");
                mForward.setVisibility(View.VISIBLE);
                mFilterText.setVisibility(View.VISIBLE);
                ToastUtil.getInstance().toast("解绑成功");
                break;
            case 3:
                //mBindCarFacility.setText(bean.beanJson);
                mBindCarFacility.setEnabled(false);
                mxinghao.setVisibility(View.VISIBLE);
                mchejiahao.setVisibility(View.VISIBLE);
                myimei.setVisibility(View.VISIBLE);
               //mchexinghao.setText(info.getPinpaiName());
               // mchejia.setText(info.getChejiaNum());
               // mimei.setText(info.getImei());
                mBindCarFacility.setVisibility(View.GONE);
                mBindCarBt.setText("解绑");
                mForward.setVisibility(View.GONE);
                mFilterText.setVisibility(View.GONE);
                ToastUtil.getInstance().toast("绑定成功");
                startToActivity();
                break;
            case 4:
                ToastUtil.getInstance().toast("设备号不正确");
                break;


        }
        } catch (Exception e) {
            e.printStackTrace();
        }


    /*    if (bean.getStatusCode()==IConstants.BindSucceed){
            ToastUtil.getInstance().toast("绑定成功");
            //存储一下设备号
            SPUtil.getInstance().setData("facilityCode",mFacilityCode);


            isBind=true;
            SPUtil.getInstance().setBoolean("isBind",isBind);
            startToActivity();
        }else if(bean.getStatusCode()==IConstants.BindFail){
            ToastUtil.getInstance().toast("该设备已被绑定");
            isBind=false;
            return;
        }*/
    }

    private void startToActivity() {
        Intent intent = new Intent(BindCarAty.this, HomeAty.class);
        startActivity(intent);
        finish();
    }


    @Override
    public void requestFail(RequestBean bean, Config config) {
        LogUtil.e("requestFail");
    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {
        LogUtil.e("requestTokenInvalid");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.forward:
                //扫一扫
                Intent intent = new Intent(BindCarAty.this,CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SCAN);
                break;
            case R.id.bind_car_bt:
                //解绑
            mPhone = SPUtil.getInstance().getData("phone");
            SPUtil.getInstance().setData("status",mBindCarBt.getText().toString());
                if(mBindCarFacility.getText().toString().trim().length()<15&&mBindCarBt.getText().equals("绑定")){
                    ToastUtil.getInstance().toast("设备号输入错误");
                    break;
                }
            mFacilityCode = mBindCarFacility.getText().toString().trim();
            BindFacilityCode();
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(DECODED_CONTENT_KEY);
                Bitmap bitmap = data.getParcelableExtra(DECODED_BITMAP_KEY);
                mBindCarFacility.setText(content);
            }
        }
    }
}
